package com.example.musiccommunity.ManagerPage;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.example.musiccommunity.MainActivity;
import com.example.musiccommunity.R;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class AddNotice extends AppCompatActivity {

    private Toolbar toolbar;
    private EditText title;
    private EditText addContent;
    private Button ok;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_notice);

        toolbar = findViewById(R.id.toolbar);
        title = findViewById(R.id.title);
        addContent = findViewById(R.id.add_content);
        ok = findViewById(R.id.ok);
        toolbar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        ok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(addContent.getText().toString().equals("")
                        ||title.getText().toString().equals("")){
                    Toast.makeText(AddNotice.this, "请填写完整", Toast.LENGTH_SHORT).show();
                    return ;
                }
                reqString();

            }
        });


    }
    void reqString(){
        String url= MainActivity.ip+"/notice/add";

        SimpleDateFormat formatter= new SimpleDateFormat("yyyy-MM-dd HH:mm");
        Date date = new Date(System.currentTimeMillis());

        StringRequest stringRequest = new StringRequest(Request.Method.POST, url,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        // 处理响应
                        String s=response;
                        Log.d("***********",s.toString());
                        if(s.toString().equals("success")){
                            Toast.makeText(AddNotice.this, "新增成功", Toast.LENGTH_SHORT).show();
                            finish();
                        }else{
                            Toast.makeText(AddNotice.this, "新增失败!", Toast.LENGTH_SHORT).show();
                        }
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        // 处理错误
                        Log.d("--------",error.getMessage().toString());
                        Toast.makeText(AddNotice.this, "新增失败", Toast.LENGTH_SHORT).show();
                    }
                }) {
            @Override
            protected Map<String, String> getParams() {

                Map<String, String> params = new HashMap<>();
                params.put("title", title.getText().toString());
                params.put("txt",addContent.getText().toString());
                params.put("time", formatter.format(date));
                return params;
            }
        };
        MainActivity.requestQueue.add(stringRequest);
    }

}